/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

package hbi.domain.java;

import hbi.wire.java.guest.GuestEnd;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class LocalAgentStub implements InvocationHandler
{

    private final GuestEnd guestEnd;

    public LocalAgentStub(GuestEnd guestEnd)
    {
        this.guestEnd = guestEnd;
    }

    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable
    {
        guestEnd.scriptAgent(method, args);

        Class<?> returnType = method.getReturnType();
        if (!returnType.isPrimitive()) return null;
        if (returnType == Void.TYPE) return null;
        // relies on auto-boxing for performance
        if (returnType == Boolean.TYPE) return false;
        if (returnType == Integer.TYPE) return 0;
        if (returnType == Long.TYPE) return 0L;
        if (returnType == Double.TYPE) return 0.0D;
        if (returnType == Float.TYPE) return (float) 0.0;
        if (returnType == Byte.TYPE) return (byte) 0;
        if (returnType == Short.TYPE) return (short) 0;
        if (returnType == Character.TYPE) return (char) 0;
        return null;
    }

}
